package com.softeem.music_sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.music_sys.comm.Result;
import com.softeem.music_sys.entity.Album;
import com.softeem.music_sys.service.AlbumService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "专辑接口", description = "专辑相关操作：增删改查等")
@RestController
@RequestMapping("/album")
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    // 添加专辑
    @Operation(summary = "添加专辑", description = "添加专辑，提供JSON格式专辑数据")
    @PostMapping
    public Result add(@RequestBody Album album) {
        boolean flag = albumService.save(album);
        return flag ? Result.success().setMsg("添加成功").setData(album) : Result.fail("添加失败");
    }

    // 修改专辑信息
    @Operation(summary = "修改专辑信息", description = "修改专辑信息，提供JSON格式专辑数据")
    @PutMapping
    public Result update(@RequestBody Album album) {
        boolean flag = albumService.updateById(album);
        return flag ? Result.success().setMsg("修改成功") : Result.fail("修改失败");
    }

    // 删除专辑
    @Operation(summary = "删除专辑", description = "根据专辑ID删除专辑")
    @Parameters({
            @Parameter(name = "id", description = "专辑ID")
    })
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        // 检查专辑是否存在
        Album album = albumService.getById(id);
        if (album == null) {
            return Result.fail("专辑不存在");
        }
        boolean flag = albumService.removeById(id);
        return flag ? Result.success().setMsg("删除成功") : Result.fail("删除失败");
    }

    // 专辑列表查询（分页）
    @Operation(summary = "专辑列表查询", description = "专辑列表查询，支持分页")
    @Parameters({
            @Parameter(name = "page", description = "页码"),
            @Parameter(name = "limit", description = "每页数量")
    })
    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit
    ) {
        Page<Album> pageUtil = new Page<>(page, limit);
        List<Album> list = albumService.list(pageUtil);
        return Result.success().setData(list);
    }

    // 根据ID获取专辑信息
    @Operation(summary = "获取专辑信息", description = "根据专辑ID获取专辑详细信息")
    @Parameters({
            @Parameter(name = "id", description = "专辑ID")
    })
    @GetMapping("/{id}")
    public Result getAlbumById(@PathVariable Integer id) {
        Album album = albumService.getById(id);
        if (album == null) {
            return Result.fail("专辑不存在");
        }
        return Result.success().setData(album);
    }

    // 根据歌手ID查询专辑列表
    @Operation(summary = "根据歌手ID查询专辑", description = "根据歌手ID查询该歌手的所有专辑")
    @Parameters({
            @Parameter(name = "singerId", description = "歌手ID")
    })
    @GetMapping("/bySinger/{singerId}")
    public Result getAlbumBySingerId(@PathVariable Integer singerId) {
        List<Album> list = albumService.list(new com.baomidou.mybatisplus.core.conditions.query.QueryWrapper<Album>().eq("singer_id", singerId));
        return Result.success().setData(list);
    }
}